package com.arrogames.blondbattle
{
	import com.arrogames.blondbattle.assets.AssetManager;
	import flash.text.TextField;
	import org.as3commons.logging.api.getLogger;
	import org.as3commons.logging.api.ILogger;
	
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.geom.Rectangle;
	import flash.ui.Multitouch;
	import flash.ui.MultitouchInputMode;
	
	import org.as3commons.logging.api.LOGGER_FACTORY;
	import org.as3commons.logging.setup.LogSetupLevel;
	import org.as3commons.logging.setup.RegExpSetup;
	import org.as3commons.logging.setup.SimpleTargetSetup;
	import org.as3commons.logging.setup.target.TraceTarget;
	
	import org.gestouch.core.Gestouch;
	import org.gestouch.extensions.starling.StarlingDisplayListAdapter;
	import org.gestouch.extensions.starling.StarlingTouchHitTester;
	import org.gestouch.input.NativeInputAdapter;
	
	import starling.core.Starling;
	import starling.display.DisplayObject;
	import starling.utils.RectangleUtil;
	import starling.utils.ScaleMode;
	import starling.events.Event;
	
	/**
	 * ...
	 * @author Robin van den Bogaard
	 */
	[SWF(backgroundColor='0x000000', width='800', height='450')]
	[Frame(factoryClass="com.arrogames.blondbattle.preloader.BBPreloader")]
	public class Main extends Sprite {
		
		private static const logger:ILogger = getLogger(Main);
		
		public static const WIDTH:Number = 800;
		public static const HEIGHT:Number = 450;
		
		private var myStarling:Starling;
		
		private var gameMain:BBMain = null;
		
		public function Main() {
			if (stage) { init(); }
			else { addEventListener(flash.events.Event.ADDED_TO_STAGE, init); }
		}
		
		private function init(event:flash.events.Event = null):void {
			removeEventListener(flash.events.Event.ADDED_TO_STAGE, init);
			
			setupLogging();
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			Starling.handleLostContext = true;
			
			var stageWidth:int  = WIDTH;
			var stageHeight:int = HEIGHT;;
			
			//viewport to make the stage as big as possible and of proper scale on all devices
			var viewPort:Rectangle = RectangleUtil.fit(
				new Rectangle(0, 0, stageWidth, stageHeight), 
				new Rectangle(0, 0, stage.fullScreenWidth, stage.fullScreenHeight), 
				ScaleMode.SHOW_ALL);
			
			// entry point
			myStarling = new Starling(BBMain, stage, viewPort);
			myStarling.stage.stageWidth = stageWidth;
			myStarling.stage.stageHeight = stageHeight;
			myStarling.start();
			myStarling.addEventListener(starling.events.Event.ROOT_CREATED, onRootCreated);
			
			
			// touch or gesture?
			Multitouch.inputMode = MultitouchInputMode.GESTURE;
			
			//setup gestouch to work with starling: see "https://github.com/fljot/Gestouch" - "Advanced usage: Starling, ..."
			Gestouch.inputAdapter ||= new NativeInputAdapter(stage);
			Gestouch.addDisplayListAdapter(DisplayObject, new StarlingDisplayListAdapter());
			Gestouch.addTouchHitTester(new StarlingTouchHitTester(myStarling), -1);
			
			//will pause everything when pressing the home or back button.
			stage.addEventListener(flash.events.Event.DEACTIVATE, stageDeactivateHandler, false, 0, true);
		}
		
		private function startGame():void {
			
		}
		
		
		private function setupLogging():void {
			var logTarget:TraceTarget = new TraceTarget();
			
			LOGGER_FACTORY.setup = new RegExpSetup()
				  .addTargetRule( /^com\.arrogames\.blondbattle\./, logTarget, LogSetupLevel.INFO );
				  //.addTargetRule( /^com\.arrogames\.blondbattle\.game\.rendering\.CameraRenderSystem/, logTarget, LogSetupLevel.DEBUG );
			logger.debug("Loggin setup");
		}
		
		private function onRootCreated(event:starling.events.Event, game:BBMain):void {
			logger.debug("Root Created");
			this.gameMain = game;
			
			myStarling.removeEventListener(starling.events.Event.ROOT_CREATED, onRootCreated);
			
			AssetManager.instance.enqueueAll();
			AssetManager.instance.loadQueue(onProgress);
		}
		
		private function onProgress(ratio:Number):void {
			logger.debug("Loading assets, progress: {0}", [ratio]);
			gameMain.assetProgress(ratio);
			
			if (ratio == 1) {
				gameMain.initialize();
			}
		}
		
		private function stageDeactivateHandler(event:flash.events.Event):void {
			logger.debug("Stage Deactivated");
			this.myStarling.stop();
			this.stage.addEventListener(flash.events.Event.ACTIVATE, stageActivateHandler, false, 0, true);
		}
		
		private function stageActivateHandler(event:flash.events.Event):void {
			logger.debug("Stage Activated");
			this.stage.removeEventListener(flash.events.Event.ACTIVATE, stageActivateHandler);
			this.myStarling.start();
		}
		
	}
	
}